package com.cjy.runnable.demo9;

/**
 * @author Cheng JiYe
 * @description:
 * @date 2021/4/25 1:28
 */
public class MyThread implements Runnable {
  /** 假设一共有5张票 */
  private int ticket = 5;

  @Override
  public void run() {
    for (int i = 0; i < 100; i++) {
      // 要对当前对象进行同步
      synchronized (this) {
        // 还有票
        if (ticket > 0) {
          try {
            // 加入延迟
            Thread.sleep(300);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          System.out.println("卖票：ticket = " + ticket--);
        }
      }
    }
  }
}
